package com.loongtech.bi.manager.player;

import com.loongtech.bi.action.PageHelper;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 玩家分析-留存分析manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class OnlinePlayerManager extends ManagerQueryCacheBase<Object> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private AcuManager acuManager;
	@Autowired
	private PcuManager pcuManager;
	@Autowired
	private DaPlayTimeManager daPlayTimeManager;
	
	//趋势图
	public List<OnlieTendData> getOnlieTendData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		List<OnlieTendData> result = new ArrayList<OnlieTendData>();
		//map
		Map<String,OnlieTendData> map = new HashMap<String,OnlieTendData>();
		//acu
		List<Object[]> acuList = acuManager.getAcuByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		for(Object[] o : acuList) {
			OnlieTendData OnlieTendData = new OnlieTendData();
			OnlieTendData.setDay(o[0].toString());
			OnlieTendData.setAcu(Long.parseLong(o[1].toString()));
			//map 存入
			map.put(o[0].toString(), OnlieTendData);
		}
		
		//pcu
		List<Object[]> list = pcuManager.getPcuByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		for(Object[] o : list) {
			OnlieTendData OnlieTendData = map.get(o[0].toString());
			
			if(OnlieTendData != null) {
				OnlieTendData.setPcu(Long.parseLong(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlieTendData);
			}
		}
		
		//平均在线时长
		list = daPlayTimeManager.getDaPlayTimeByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for(Object[] o : list) {
			OnlieTendData OnlieTendData = map.get(o[0].toString());
			
			if(OnlieTendData != null) {
				OnlieTendData.setDaPlayTime(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlieTendData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : acuList) {
			OnlieTendData OnlieTendData = map.get(o[0].toString());
			
			if(OnlieTendData != null) {
				//设置acu/pcu
				Float f = (float)(OnlieTendData.getPcu() == 0 ? 0 : OnlieTendData.getAcu() * 1.0f / OnlieTendData.getPcu());
				OnlieTendData.setAcu_puc(PageHelper.floatKeepTwoNumber(f));
				result.add(OnlieTendData);
			}
		}
		
		return result;
	}
	
	//平台对比
	public List<OnlinePlatFormData> getOnlinePlatFormData(Date dateStart, Date dateEnd) {
		List<OnlinePlatFormData> result = new ArrayList<OnlinePlatFormData>();
		//map
		Map<String,OnlinePlatFormData> map = new HashMap<String,OnlinePlatFormData>();
		//acu
		List<Object[]> acuList = acuManager.getAcuGroupByPlatform(dateStart, dateEnd);
		
		for(Object[] o : acuList) {
			OnlinePlatFormData OnlinePlatFormData = new OnlinePlatFormData();
			OnlinePlatFormData.setPlatformId(o[0].toString());
			OnlinePlatFormData.setAcu(Long.parseLong(o[1].toString()));
			//map 存入
			map.put(o[0].toString(), OnlinePlatFormData);
		}
		
		//pcu
		List<Object[]> list = pcuManager.getPcuGroupByPlatform(dateStart, dateEnd);
		
		for(Object[] o : list) {
			OnlinePlatFormData OnlinePlatFormData = map.get(o[0].toString());
			
			if(OnlinePlatFormData != null) {
				OnlinePlatFormData.setPcu(Long.parseLong(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlinePlatFormData);
			}
		}
		
		//平均在线时长
		list = daPlayTimeManager.getDaPlayTimeGroupByPlatform(dateStart, dateEnd);
		for(Object[] o : list) {
			OnlinePlatFormData OnlinePlatFormData = map.get(o[0].toString());
			
			if(OnlinePlatFormData != null) {
				OnlinePlatFormData.setDaPlayTime(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlinePlatFormData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : acuList) {
			OnlinePlatFormData OnlinePlatFormData = map.get(o[0].toString());
			
			if(OnlinePlatFormData != null) {
				//设置acu/pcu
				Float f = (float)(OnlinePlatFormData.getPcu() == 0 ? 0 : OnlinePlatFormData.getAcu() * 1.0f / OnlinePlatFormData.getPcu());
				OnlinePlatFormData.setAcu_puc(PageHelper.floatKeepTwoNumber(f));
				result.add(OnlinePlatFormData);
			}
		}
		
		return result;
	}
	
	//渠道对比
	public List<OnlineChannelData> getOnlineChannelData(Date dateStart, Date dateEnd) {
		List<OnlineChannelData> result = new ArrayList<OnlineChannelData>();
		//map
		Map<String,OnlineChannelData> map = new HashMap<String,OnlineChannelData>();
		//acu
		List<Object[]> acuList = acuManager.getAcuGroupByChannel(dateStart, dateEnd);
		
		for(Object[] o : acuList) {
			OnlineChannelData OnlineChannelData = new OnlineChannelData();
			OnlineChannelData.setChannelId(o[0].toString());
			OnlineChannelData.setAcu(Long.parseLong(o[1].toString()));
			//map 存入
			map.put(o[0].toString(), OnlineChannelData);
		}
		
		//pcu
		List<Object[]> list = pcuManager.getPcuGroupByChannel(dateStart, dateEnd);
		
		for(Object[] o : list) {
			OnlineChannelData OnlineChannelData = map.get(o[0].toString());
			
			if(OnlineChannelData != null) {
				OnlineChannelData.setPcu(Long.parseLong(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlineChannelData);
			}
		}
		
		//平均在线时长
		list = daPlayTimeManager.getDaPlayTimeGroupByChannel(dateStart, dateEnd);
		for(Object[] o : list) {
			OnlineChannelData OnlineChannelData = map.get(o[0].toString());
			
			if(OnlineChannelData != null) {
				OnlineChannelData.setDaPlayTime(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlineChannelData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : acuList) {
			OnlineChannelData OnlineChannelData = map.get(o[0].toString());
			
			if(OnlineChannelData != null) {
				//设置acu/pcu
				Float f = (float)(OnlineChannelData.getPcu() == 0 ? 0 : OnlineChannelData.getAcu() * 1.0f / OnlineChannelData.getPcu());
				OnlineChannelData.setAcu_puc(PageHelper.floatKeepTwoNumber(f));
				result.add(OnlineChannelData);
			}
		}
		
		return result;
	}
	
	//区服对比
	public List<OnlineServerData> getOnlineServerData(Date dateStart, Date dateEnd) {
		List<OnlineServerData> result = new ArrayList<OnlineServerData>();
		//map
		Map<String,OnlineServerData> map = new HashMap<String,OnlineServerData>();
		//acu
		List<Object[]> acuList = acuManager.getAcuGroupByServer(dateStart, dateEnd);
		
		for(Object[] o : acuList) {
			OnlineServerData OnlineServerData = new OnlineServerData();
			OnlineServerData.setServerId(o[0].toString());
			OnlineServerData.setAcu(Long.parseLong(o[1].toString()));
			//map 存入
			map.put(o[0].toString(), OnlineServerData);
		}
		
		//pcu
		List<Object[]> list = pcuManager.getPcuGroupByServer(dateStart, dateEnd);
		
		for(Object[] o : list) {
			OnlineServerData OnlineServerData = map.get(o[0].toString());
			
			if(OnlineServerData != null) {
				OnlineServerData.setPcu(Long.parseLong(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlineServerData);
			}
		}
		
		//平均在线时长
		list = daPlayTimeManager.getDaPlayTimeGroupByServer(dateStart, dateEnd);
		for(Object[] o : list) {
			OnlineServerData OnlineServerData = map.get(o[0].toString());
			
			if(OnlineServerData != null) {
				OnlineServerData.setDaPlayTime(Integer.parseInt(o[1].toString()));
				//map 存入
				map.put(o[0].toString(), OnlineServerData);
			}
		}
		
		//遍历存入结果
		for(Object[] o : acuList) {
			OnlineServerData OnlineServerData = map.get(o[0].toString());
			
			if(OnlineServerData != null) {
				//设置acu/pcu
				Float f = (float)(OnlineServerData.getPcu() == 0 ? 0 : OnlineServerData.getAcu() * 1.0f / OnlineServerData.getPcu());
				OnlineServerData.setAcu_puc(PageHelper.floatKeepTwoNumber(f));
				result.add(OnlineServerData);
			}
		}
		
		return result;
	}
	
	//趋势的图表
	public class OnlieTendData {
		private String platformId;
		private String channelId;
		private String serverId;
		
		@ChartFlow(name="日期")
		private String day; // 日期
		@ChartSelect(name = "pcu", key = 1)
		@ChartData1(name = "pcu", key = 1)
		private long pcu; //
		@ChartSelect(name = "acu", key = 2)
		@ChartData1(name = "acu", key = 2)
		private long acu; //
		@ChartSelect(name = "ACU/PCU", key = 3)
		@ChartData1(name = "ACU/PCU", key = 3)
		private float acu_puc;// acu/puc
		@ChartSelect(name = "平均在线时长(分钟)", key = 4)
		@ChartData1(name = "平均在线时长(分钟)", key = 4)
		private int daPlayTime;
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId;
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public long getPcu() {
			return pcu;
		}
		public void setPcu(long pcu) {
			this.pcu = pcu;
		}
		public long getAcu() {
			return acu;
		}
		public void setAcu(long acu) {
			this.acu = acu;
		}
		public float getAcu_puc() {
			return acu_puc;
		}
		public void setAcu_puc(float acu_puc) {
			this.acu_puc = acu_puc;
		}
		public int getDaPlayTime() {
			return daPlayTime;
		}
		public void setDaPlayTime(int daPlayTime) {
			this.daPlayTime = daPlayTime;
		}
	}
	
	//平台对比
	public class OnlinePlatFormData {
		private String platformId;
		@ChartFlow(name="平台")
		private String platformName;
		private String channelId;
		private String serverId;
		private String day; // 日期
		@ChartSelect(name = "pcu", key = 1)
		@ChartData1(name = "pcu", key = 1)
		private long pcu; //
		@ChartSelect(name = "acu", key = 2)
		@ChartData1(name = "acu", key = 2)
		private long acu; //
		@ChartSelect(name = "ACU/PCU", key = 3)
		@ChartData1(name = "ACU/PCU", key = 3)
		private float acu_puc;// acu/puc
		@ChartSelect(name = "平均在线时长(分钟)", key = 4)
		@ChartData1(name = "平均在线时长(分钟)", key = 4)
		private int daPlayTime;
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
			this.platformName = BiPlatformManager.getPlatformNameById(platformId);
		}
		public String getChannelId() {
			return channelId; 
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public long getPcu() {
			return pcu;
		}
		public void setPcu(long pcu) {
			this.pcu = pcu;
		}
		public long getAcu() {
			return acu;
		}
		public void setAcu(long acu) {
			this.acu = acu;
		}
		public float getAcu_puc() {
			return acu_puc;
		}
		public void setAcu_puc(float acu_puc) {
			this.acu_puc = acu_puc;
		}
		public int getDaPlayTime() {
			return daPlayTime;
		}
		public void setDaPlayTime(int daPlayTime) {
			this.daPlayTime = daPlayTime;
		}
		@Override
		public String toString() {
			return "OnlinePlatFormData [platformId=" + platformId
					+ ", channelId=" + channelId + ", serverId=" + serverId
					+ ", day=" + day + ", pcu=" + pcu + ", acu=" + acu
					+ ", acu_puc=" + acu_puc + ", daPlayTime=" + daPlayTime
					+ "]";
		}
		public String getPlatformName() {
			return platformName;
		}
		public void setPlatformName(String platformName) {
			this.platformName = platformName;
		}
	}
	
	//渠道对比
	public class OnlineChannelData {
		private String platformId;
		private String channelId;
		@ChartFlow(name="渠道")
		private String channelName;
		private String serverId;
		private String day; // 日期
		@ChartSelect(name = "pcu", key = 1)
		@ChartData1(name = "pcu", key = 1)
		private long pcu; //
		@ChartSelect(name = "acu", key = 2)
		@ChartData1(name = "acu", key = 2)
		private long acu; //
		@ChartSelect(name = "ACU/PCU", key = 3)
		@ChartData1(name = "ACU/PCU", key = 3)
		private float acu_puc;// acu/puc
		@ChartSelect(name = "平均在线时长(分钟)", key = 4)
		@ChartData1(name = "平均在线时长(分钟)", key = 4)
		private int daPlayTime;
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId; 
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
			this.channelName = BiPlatformManager.getChannelNameById(channelId);
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public long getPcu() {
			return pcu;
		}
		public void setPcu(long pcu) {
			this.pcu = pcu;
		}
		public long getAcu() {
			return acu;
		}
		public void setAcu(long acu) {
			this.acu = acu;
		}
		public float getAcu_puc() {
			return acu_puc;
		}
		public void setAcu_puc(float acu_puc) {
			this.acu_puc = acu_puc;
		}
		public int getDaPlayTime() {
			return daPlayTime;
		}
		public void setDaPlayTime(int daPlayTime) {
			this.daPlayTime = daPlayTime;
		}
		@Override
		public String toString() {
			return "OnlinePlatFormData [platformId=" + platformId
					+ ", channelId=" + channelId + ", serverId=" + serverId
					+ ", day=" + day + ", pcu=" + pcu + ", acu=" + acu
					+ ", acu_puc=" + acu_puc + ", daPlayTime=" + daPlayTime
					+ "]";
		}
		public String getChannelName() {
			return channelName;
		}
		public void setChannelName(String channelName) {
			this.channelName = channelName;
		}
	}
	
	//区服对比
	public class OnlineServerData {
		private String platformId;
		private String channelId;
		private String serverId;
		@ChartFlow(name="区服")
		private String serverName;
		private String day; // 日期
		@ChartSelect(name = "pcu", key = 1)
		@ChartData1(name = "pcu", key = 1)
		private long pcu; //
		@ChartSelect(name = "acu", key = 2)
		@ChartData1(name = "acu", key = 2)
		private long acu; //
		@ChartSelect(name = "ACU/PCU", key = 3)
		@ChartData1(name = "ACU/PCU", key = 3)
		private float acu_puc;// acu/puc
		@ChartSelect(name = "平均在线时长(分钟)", key = 4)
		@ChartData1(name = "平均在线时长(分钟)", key = 4)
		private int daPlayTime;
		
		public String getPlatformId() {
			return platformId;
		}
		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		public String getChannelId() {
			return channelId; 
		}
		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}
		public String getServerId() {
			return serverId;
		}
		public void setServerId(String serverId) {
			this.serverId = serverId;
			this.serverName = BiPlatformManager.getServerNameById(serverId);
		}
		public String getDay() {
			return day;
		}
		public void setDay(String day) {
			this.day = day;
		}
		public long getPcu() {
			return pcu;
		}
		public void setPcu(long pcu) {
			this.pcu = pcu;
		}
		public long getAcu() {
			return acu;
		}
		public void setAcu(long acu) {
			this.acu = acu;
		}
		public float getAcu_puc() {
			return acu_puc;
		}
		public void setAcu_puc(float acu_puc) {
			this.acu_puc = acu_puc;
		}
		public int getDaPlayTime() {
			return daPlayTime;
		}
		public void setDaPlayTime(int daPlayTime) {
			this.daPlayTime = daPlayTime;
		}
		
		@Override
		public String toString() {
			return "OnlinePlatFormData [platformId=" + platformId
					+ ", channelId=" + channelId + ", serverId=" + serverId
					+ ", day=" + day + ", pcu=" + pcu + ", acu=" + acu
					+ ", acu_puc=" + acu_puc + ", daPlayTime=" + daPlayTime
					+ "]";
		}
		public String getServerName() {
			return serverName;
		}
		public void setServerName(String serverName) {
			this.serverName = serverName;
		}
	}
}
